package com.vertigrated.db.adventureworks;

import com.vertigrated.db.Database;
import com.vertigrated.db.DeleteBuilder;
import com.vertigrated.orm.AbstractFactory;

import javax.sql.RowSet;
import java.sql.SQLException;

public class PersonContactFactory extends AbstractFactory<PersonContact, Integer>
{
    private Database db;

    public PersonContactFactory(final Database db)
    {
        this.db = db;
    }

    protected void mapRowSetToObject(final RowSet rs)
    {
        try
        {
            final PersonContact pc = new PersonContact(rs.getInt("ContactID"));
            pc.setFirstName(rs.getString("FirstName"));
            pc.setMiddleName(rs.getString("MiddleName"));
            pc.setLastName(rs.getString("LastName"));
            //TODO: finish mapping the RowSet to PersonContact
        }
        catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
    }

    public boolean store(final PersonContact object)
    {
        //TODO: implement this method
        throw new UnsupportedOperationException("com.vertigrated.db.adventureworks.PersonContactFactory.store is not implemented yet!");
    }

    public boolean delete(final PersonContact pc)
    {
        final DeleteBuilder delb = this.db.createDeleteBuilder("Person", "Contact");
        delb.addWhereEqual("ContactID", pc.getContactId());
        return this.db.execute(delb) == 1;
    }

    public PersonContact getByPrimaryKey(final Integer key)
    {
        //TODO: implement this method
        throw new UnsupportedOperationException("com.vertigrated.db.adventureworks.PersonContactFactory.getByPrimaryKey is not implemented yet!");
    }
}
